﻿<!--
    Copyright (C) 2011 Ari Rokosa, Chinedu Eze, Johannes Virtanen,
                       Petri Tuononen, Timo Korkalainen

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
@{
    Layout = null;
 }

@model IEnumerable<sdcsd.Models.DesktopItemModel>

@foreach (var item in Model) {
    if (ViewBag.ActiveDesktop == item.DesktopID)
    {
        <div class="draggable" title="@item.Name" style="
         left: @item.LocX; top: @item.LocY">`
            <img class="fileIcon" src="../../Content/icons/text.png" name="@item.Name" alt="@item.Name"/>
            <script type="text/javascript">
                document.getElementsByName("@item.Name")[0].src = getImgUrl("@item.Name");
            </script>
            <p class="fileName">@item.Name</p>
        </div>    
    }
}

@{if (ViewBag.login == true)
  {
<script type="text/javascript">
    $(function () {
        $(".draggable").draggable({
            containment: '#desktop-container',
            cursor: 'move',
            snap: '#desktop-container',
            drag: function (event, ui) {
                this.style.opacity = 0.5
                dragging = true;
            },
            stop: function (event, ui) {
                this.style.opacity = 1.0

                var width = $('#desktop-container').width();
                var height = $('#desktop-container').height();
                var left = 100 - (width - $(this).position().left) / width * 100;
                var top = 100 - (height - $(this).position().top) / height * 100;

                var locX = left + "";
                var locY = top + "";
                locX = locX.substr(0, 6) + "%";
                locY = locY.substr(0, 6) + "%";
                dragging = false;

                $.post("Desktop/UpdateLocation?name=" + $(this).attr("title") + "&locX=" + locX + "&locY=" + locY, function () { });
            }
        });
    });

    $(function () {
        $('.draggable').mousedown(function (event) {
            if (event.which == 2) { //middle mouse button
                $.post("Desktop/DeleteItem?name=" + $(this).attr("title"), function () { refreshDesktop(); });
            }
        });
    });

</script>

  }}

 <script type="text/javascript">

     dragging = false;

     $(function () {
         $('.draggable').dblclick(function () {
             window.open("/Desktop/GetFileFromDisk/" + this.title);
         });
     });

     function refreshDesktop() {
         if (dragging == false) {
             $.post("Desktop/Index", function (data) {
                 $('#desktop-container').html(data);
             });
         }
     };
</script>

<script type="text/javascript">
    function getImgUrl(fileName) {
        var imgSrc;
        var dir = "../../Content/icons/";
        var ext = fileName.split('.').pop();
        if (ext == "txt") {
            imgSrc = dir + "text.png"
        }
        else if (ext == "doc" || ext == "docx" || ext == "xls" || ext == "xlsx") {
            imgSrc = dir + "document.png"
        }
        else if (ext == "zip" || ext == "tar.gz" || ext == "rar") {
            imgSrc = dir + "archive.png"
        }
        else if (ext == "mp3" || ext == "wmv") {
            imgSrc = dir + "audio.png"
        }
        else if (ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "bmp" || ext == "gif") {
            imgSrc = dir + "image.png"
        }
        else if (ext == "mpeg" || ext == "avi") {
            imgSrc = dir + "video.png"
        }
        else {
            imgSrc = dir + "text.png"
        }
        return imgSrc;
    };
</script>
